/**
 * Created by scenic on 2017/8/10.
 */
function UpdateSegmentInterface() {

}

UpdateSegmentInterface.prototype = {
    constructor: UpdateSegmentInterface,
    //计算当前空间的 剩余空间
    calculateFreeSpace: function (space) {

    },


    //更新位置
    calculateFirstChildSpacePosition: function () {

    }
}

function UpdateSegmentInterfaceForBox() {
    UpdateSegmentInterface.call(this);
}
var DEBUG = false;
UpdateSegmentInterfaceForBox.prototype = Object.assign(Object.create(UpdateSegmentInterface.prototype), {
    constructor: UpdateSegmentInterfaceForBox,

    calculateFreeSpace: function (space) {
        if (space != this) {
            console.error("===")
        }
        var spaceFreeDimension = this.dimension.clone();
        this.faces.forEach(function (face) {
            if (!face.associatePlank.isDoor) {
                var thickness = face.getAssociateThickness();
                var offset = new THREE.Vector3(thickness, thickness, thickness);
                spaceFreeDimension.sub(offset.multiply(face.normal).positive());
            }
        })
        return spaceFreeDimension;
    },
    calculateFirstChildSpacePosition: function (spaceFreeDimension) {


        var directionNormal = this.segmentOrientation;

        // TODO(liuzd) 临时这样计算，后续的话要优化
        var startFace = this.getBeginFaceByDirection(directionNormal);
        var thickness = new THREE.Vector3();
        this.faces.forEach(function (face) {
            var directionNegate = directionNormal.clone().negate();
            if (face.normal.equals(directionNegate) ||
                (face.normal.dot(directionNegate) == 0 && face.normal.isContainNegative())

            ) {
                //TODO(liuzd) 这里注意，如果当前的face是门板的话，这里会有错误的，因为门板对面来说没有厚度
                if (face.associatePlank.isDoor) {
                    throw new Error("not door");
                }
                console.log(face);
                console.log(thickness)
                var thickness_ = face.getAssociateThickness();
                thickness.add(new THREE.Vector3(thickness_, thickness_, thickness_).multiply(face.normal.clone().positive()));
            }
        })

        var startPosition = startFace.box3.getCenter();
        if (DEBUG) {
            console.log("startPosition is ");
            console.log(startPosition);
        }
        //减去在方向轴方向多加出来的
        //坐标需要从坐标开始
        var dimensionOffSet = new THREE.Vector3().subVectors(spaceFreeDimension, this.dimension).multiplyScalar(0.5);
        if (DEBUG) {
            console.log("dimension offset is ");
            console.log(dimensionOffSet.clone());
        }
        //在分割方向上，位置不需要平移
        dimensionOffSet.sub(dimensionOffSet.clone().projectOnVector(directionNormal));
        //和非分割方向的边缘对其
        startPosition.add(dimensionOffSet);

        //减去对应其边缘面的厚度
        var thicknessProject = thickness.clone().projectOnVector(directionNormal);
        if (DEBUG) {
            console.log("thicknessProject ");
            console.log(thicknessProject.clone());
        }

        var thicknessMultiplyNormal = thickness.clone().multiply(directionNormal);
        if (DEBUG) {
            console.log("thicknessMultiplyNormal is ");
            console.log(thicknessMultiplyNormal);
        }
        thickness.sub(thicknessProject).add(thicknessMultiplyNormal);
        startPosition.add(thickness);

        if (DEBUG) {
            console.log("thickness is ");
            console.log(thickness.clone());
        }

        startPosition.add(this.children_space[0].getHalfDimensionInDirection(directionNormal));
        return startPosition;
    }
});

export {
    UpdateSegmentInterfaceForBox
}